#include "sobel.h"
#include <stdio.h>
#include <assert.h>

int main() {
    byte rgb[RGB_SIZE];
    byte gray[IMG_PIXELS];
    byte sobel_h_res[IMG_PIXELS];
    byte sobel_v_res[IMG_PIXELS];
    byte contour_img[IMG_PIXELS];

    int has_errors = 0;
    byte check;

    FILE *input = fopen("input.data", "r");
    assert(input != NULL && "Couldn't open input data file");

    for (int i = 0; i < IMG_PIXELS; i++) {
        byte r, g, b;
        int res = fscanf(input, "%hhd %hhd %hhd\n", &r, &g, &b);
        assert(res == 3 && "read input data file failed");
        rgb[3*i + 0] = r;
        rgb[3*i + 1] = g;
        rgb[3*i + 2] = b;
    }
    fclose(input);

    sobelFilter(rgb, gray, sobel_h_res, sobel_v_res, contour_img);

    FILE *fp = fopen("check.data", "r");
    assert(fp != NULL && "Couldn't open check data file");

    for (int i = 0; i < IMG_HEIGHT; i++) {
        for (int j = 0; j < IMG_WIDTH; j++) {
            int res = fscanf(fp, "%hhd", &check);
            assert(res == 1 && "read check data file failed");
            if (contour_img[i*IMG_WIDTH + j] != check) {
                printf("contour_img[%d] = %d, expected[%d] = %d\n",
                       i*IMG_WIDTH + j,
                       contour_img[i*IMG_WIDTH + j],
                       i*IMG_WIDTH + j,
                       check);
                has_errors++;
            }
        }
    }

    fclose(fp);

    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
